package com.example.heimashop.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.heimashop.databinding.ItemOrderBinding;
import com.example.heimashop.model.Order;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 订单列表适配器
 */
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {

    private List<Order> orders = new ArrayList<>();
    private OnOrderItemListener listener;

    public interface OnOrderItemListener {
        void onPay(Order order);
        void onCancel(Order order);
        void onConfirm(Order order);
        void onDelete(Order order);
    }

    public void setListener(OnOrderItemListener listener) {
        this.listener = listener;
    }

    public void setData(List<Order> list) {
        if (list != null) {
            this.orders = list;
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemOrderBinding binding = ItemOrderBinding.inflate(
                LayoutInflater.from(parent.getContext()),
                parent,
                false
        );
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Order order = orders.get(position);
        holder.bind(order, listener);
    }

    @Override
    public int getItemCount() {
        return orders.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private final ItemOrderBinding binding;
        private final OrderGoodsAdapter goodsAdapter;

        ViewHolder(ItemOrderBinding binding) {
            super(binding.getRoot());
            this.binding = binding;

            // 初始化商品列表
            binding.rvGoods.setLayoutManager(new LinearLayoutManager(binding.getRoot().getContext()));
            goodsAdapter = new OrderGoodsAdapter();
            binding.rvGoods.setAdapter(goodsAdapter);
        }

        void bind(Order order, OnOrderItemListener listener) {
            // 订单号
            binding.tvOrderNo.setText("订单号：" + order.getOrderNo());

            // 订单状态
            binding.tvStatus.setText(order.getStatusText());

            // 收货人信息
            binding.tvConsignee.setText(order.getConsignee() + " " + order.getPhone());
            binding.tvAddress.setText(order.getAddress());

            // 总价
            binding.tvTotalPrice.setText(String.format(Locale.CHINA, "¥%.2f", order.getTotalPrice()));

            // 商品列表
            if (order.getGoodsList() != null) {
                goodsAdapter.setData(convertToCartItems(order));
            }

            // 根据订单状态显示/隐藏按钮
            String status = order.getStatus();
            binding.btnPay.setVisibility(View.GONE);
            binding.btnCancel.setVisibility(View.GONE);
            binding.btnConfirm.setVisibility(View.GONE);
            binding.btnDelete.setVisibility(View.GONE);

            switch (status) {
                case "wait_pay":
                    // 待支付：显示去支付和取消订单
                    binding.btnPay.setVisibility(View.VISIBLE);
                    binding.btnCancel.setVisibility(View.VISIBLE);
                    break;
                case "wait_send":
                    // 待发货：无操作按钮
                    break;
                case "wait_receive":
                    // 待收货：显示确认收货
                    binding.btnConfirm.setVisibility(View.VISIBLE);
                    break;
                case "completed":
                case "cancelled":
                    // 已完成/已取消：显示删除订单
                    binding.btnDelete.setVisibility(View.VISIBLE);
                    break;
            }

            // 按钮点击事件
            binding.btnPay.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onPay(order);
                }
            });

            binding.btnCancel.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onCancel(order);
                }
            });

            binding.btnConfirm.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onConfirm(order);
                }
            });

            binding.btnDelete.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onDelete(order);
                }
            });
        }

        /**
         * 将OrderGoods转换为CartItem用于显示
         */
        private List<com.example.heimashop.model.CartItem> convertToCartItems(Order order) {
            List<com.example.heimashop.model.CartItem> cartItems = new ArrayList<>();
            for (com.example.heimashop.model.OrderGoods goods : order.getGoodsList()) {
                com.example.heimashop.model.CartItem item = new com.example.heimashop.model.CartItem();
                item.setGoodsId(goods.getGoodsId());
                item.setGoodsName(goods.getGoodsName());
                item.setGoodsPrice(goods.getGoodsPrice());
                item.setGoodsImage(goods.getGoodsImage());
                item.setGoodsCount(goods.getGoodsCount());
                cartItems.add(item);
            }
            return cartItems;
        }
    }
}
